package controllers

import (
	"cronjob/libs"
	"cronjob/models"
	"strings"
	"time"
)

// 资源管理
type ServerController struct {
	BaseController
}

func (this *ServerController) List() {
	this.Data["pageTitle"] = "执行资源管理"

	this.Data["serverGroup"] = serverGroupLists(this.serverGroups, this.userID)
	this.display()
}

func (this *ServerController) Table() {
	page, err := this.GetInt("page")
	if err != nil {
		page = 1
	}

	//每页显示几个
	limit, err := this.GetInt("limit")
	if err != nil {
		limit = 5
	}

	serverGroup := serverGroupLists(this.serverGroups, this.userID)

	this.pageSize = limit
	filters := make([]interface{}, 0)

	//
	ids := []int{0}
	filters = append(filters, "status__in", ids)

	result, count := models.TaskServerGetList(page, this.pageSize, filters...)

	list := make([]map[string]interface{}, len(result))

	/** */
	StatusText := []string{
		"<i class='fa fa-refresh' style='color:#5FB878'></i>",
		"<i class='fa fa-ban' style='color:#FF5722'></i>",
	}

	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["server_name"] = StatusText[v.Status] + "" + v.ServerName

		if serverGroup[v.GroupId] == "" {
			v.GroupId = 0
		}

		row["group_name"] = serverGroup[v.GroupId]
		row["type"] = v.Type

		row["detail"] = v.Detail
		list[k] = row
	}

	this.ajaxList("", MSG_OK, count, list)
}

func (this *ServerController) Add() {
	this.Data["pageTitle"] = "执行资源管理"

	this.Data["serverGroup"] = serverGroupLists(this.serverGroups, this.userID)

	this.display()
}

func (this *ServerController) Edit() {
	server_id, _ := this.GetInt("id")
	server, err := models.TaskSeverGetById(server_id)

	if err != nil {
		this.ajaxMsg("服务器信息部存在", MSG_ERR)

	}
	// 服务器组信息
	this.Data["serverGroup"] = serverGroupLists(this.serverGroups, this.userID)

	row := make(map[string]interface{})
	row["group_id"] = server.GroupId
	row["server_name"] = server.ServerName
	row["server_account"] = server.ServerAccount
	row["server_ip"] = server.ServerIp
	row["server_outer_ip"] = server.ServerOuterIp
	row["port"] = server.Port
	row["type"] = server.Type
	row["password"] = server.Password
	row["detail"] = server.Detail
	row["id"] = server.Id

	this.Data["server"] = row
	this.display()

}

func (this *ServerController) AjaxDel() {
	server_id, _ := this.GetInt("id")
	server, err := models.TaskSeverGetById(server_id)

	if err != nil {
		this.ajaxMsg("服务器信息部存在", MSG_ERR)
	}

	server.UpdateTime = time.Now().Unix()
	server.Status = 1

	if ok := server.Update(); ok != nil {
		this.ajaxMsg("修改失败", MSG_ERR)
	}

	this.ajaxMsg("修改成功", MSG_OK)
}

// 修改和添加
func (this *ServerController) AjaxSave() {
	server_id, _ := this.GetInt("id")

	if server_id == 0 {
		server := new(models.TaskServer)

		server.ServerName = strings.TrimSpace(this.GetString("server_name"))
		server.ServerAccount = strings.TrimSpace(this.GetString("server_account"))
		server.ServerOuterIp = strings.TrimSpace(this.GetString("server_outer_ip"))
		server.ServerIp = strings.TrimSpace(this.GetString("server_ip"))
		server.Password = strings.TrimSpace(this.GetString("password"))

		server.Detail = strings.TrimSpace(this.GetString("detail"))
		server.Type, _ = this.GetInt("type")
		server.Port, _ = this.GetInt("port")
		server.GroupId, _ = this.GetInt("group_id")

		server.CreateTime = time.Now().Unix()
		server.UpdateTime = time.Now().Unix()
		server.Status = 0

		if _, err := models.TaskServerAdd(server); err != nil {
			this.ajaxMsg(err.Error(), MSG_ERR)
		}
		this.ajaxMsg("", MSG_OK)

	}

	server, err := models.TaskSeverGetById(server_id)

	if err != nil {
		this.ajaxMsg("服务器信息部存在", MSG_ERR)
	}

	server.ServerName = strings.TrimSpace(this.GetString("server_name"))
	server.ServerAccount = strings.TrimSpace(this.GetString("server_account"))
	server.ServerOuterIp = strings.TrimSpace(this.GetString("server_outer_ip"))
	server.ServerIp = strings.TrimSpace(this.GetString("server_ip"))
	server.Password = strings.TrimSpace(this.GetString("password"))

	server.Detail = strings.TrimSpace(this.GetString("detail"))
	server.Type, _ = this.GetInt("type")
	server.Port, _ = this.GetInt("port")
	server.GroupId, _ = this.GetInt("group_id")

	server.UpdateTime = time.Now().Unix()
	server.Status = 0

	if ok := server.Update(); ok != nil {
		this.ajaxMsg("修改失败", MSG_ERR)
	}

	this.ajaxMsg("修改成功", MSG_OK)

}

func (this *ServerController) Copy() {
	this.Data["pageTitle"] = "复制服务器资源"

	server_id, _ := this.GetInt("id")
	server, err := models.TaskSeverGetById(server_id)

	if err != nil {
		this.ajaxMsg("服务器信息部存在", MSG_ERR)

	}
	// 服务器组信息
	this.Data["serverGroup"] = serverGroupLists(this.serverGroups, this.userID)

	row := make(map[string]interface{})
	row["group_id"] = server.GroupId
	row["server_name"] = server.ServerName
	row["server_account"] = server.ServerAccount
	row["server_ip"] = server.ServerIp
	row["server_outer_ip"] = server.ServerOuterIp
	row["port"] = server.Port
	row["type"] = server.Type
	row["password"] = server.Password
	row["detail"] = server.Detail
	row["id"] = server.Id

	this.Data["server"] = row
	this.display()
}

// 远程服务器资源测试
func (this *ServerController) AjaxTestServer() {
	server := new(models.TaskServer)

	server.ServerName = strings.TrimSpace(this.GetString("server_name"))
	server.ServerAccount = strings.TrimSpace(this.GetString("server_account"))
	server.ServerOuterIp = strings.TrimSpace(this.GetString("server_outer_ip"))
	server.ServerIp = strings.TrimSpace(this.GetString("server_ip"))
	server.Password = strings.TrimSpace(this.GetString("password"))
	server.Detail = strings.TrimSpace(this.GetString("detail"))
	server.Type, _ = this.GetInt("type")
	server.Port, _ = this.GetInt("port")
	server.GroupId, _ = this.GetInt("group_id")

	if server.Type == 0 {

		err := libs.RemoteCommandByPassword(server)

		if err != nil {
			this.ajaxMsg(err.Error(), MSG_ERR)
		}
		this.ajaxMsg("success", MSG_OK)
	}

	this.ajaxMsg("fail", MSG_ERR)
}
